//
// Created by Zh1an on 2023/2/11.
//

#include <pluginGl.h>

#include <Qsci/qsciscintilla.h>
#include <QWidget>
#include <QMenu>
#include <nddjsonplugin.h>
#include <functional>
#include "instanceobj.h"

#define NDD_EXPORTDLL

#if defined(Q_OS_WIN)
#if defined(NDD_EXPORTDLL)
#define NDD_EXPORT __declspec(dllexport)
#else
#define NDD_EXPORT __declspec(dllimport)
#endif
#else
#define NDD_EXPORT __attribute__((visibility("default")))
#endif

#ifdef __cplusplus
extern "C"
{
#endif

    NDD_EXPORT bool NDD_PROC_IDENTIFY(NDD_PROC_DATA *pProcData);
    NDD_EXPORT int NDD_PROC_MAIN(QWidget *pNotepad, const QString &strFileName,
                                 std::function<QsciScintilla *(QWidget*)> getCurEdit, std::function<bool(QWidget*,int, void*)> pluginCallBack, NDD_PROC_DATA *procData);

#ifdef __cplusplus
}
#endif

bool NDD_PROC_IDENTIFY(NDD_PROC_DATA *pProcData)
{
    if (pProcData == nullptr)
    {
        return false;
    }
    pProcData->m_strPlugName = QObject::tr("JsonView Plug");
    pProcData->m_strComment = QObject::tr("json format");

    pProcData->m_version = QString("0.0.0.1");
    pProcData->m_auther = QString("Zh1an");

    pProcData->m_menuType = 1;

    return true;
}

std::function<QsciScintilla *(QWidget*)> s_getCurEdit;

// 插件的入口点函数
int NDD_PROC_MAIN(QWidget *pNotepad, const QString &strFileName, std::function<QsciScintilla *(QWidget*)> getCurEdit, std::function<bool(QWidget* , int, void*)> pluginCallBack,
                  NDD_PROC_DATA *pProcData)
{
    if (pProcData == nullptr)
    {
        return 1;
    }

    s_getCurEdit = getCurEdit;

    InstanceObj* pInstance = new InstanceObj(pNotepad, pProcData->m_rootMenu, pProcData->m_strFilePath);
	pInstance->setObjectName("nddplg");

    pProcData->m_rootMenu->addAction("Show Json Table", pInstance, &InstanceObj::showJsonTable);
    pProcData->m_rootMenu->addAction("Formatting Json(Ctrl+F8)", pInstance, &InstanceObj::formatJson, Qt::CTRL + Qt::Key_F8);
    pProcData->m_rootMenu->addAction("Compress Json(Ctrl+F9)", pInstance, &InstanceObj::compressJson, Qt::CTRL + Qt::Key_F9);
    pProcData->m_rootMenu->addAction("Settings", pInstance, &InstanceObj::jsonViewSettings);


    return 0;
}
